package com.morphiastic;

import org.elasticsearch.action.delete.DeleteResponse;

public class DefaultDeleteFromIndexWorker implements IndexWorker{

	
	private IndexedClass indexedClass;
	private Object entity;
	private ElasticStore elasticStore;
	
	
	public DefaultDeleteFromIndexWorker( IndexedClass indexedClass, Object entity, ElasticStore elasticStore){
		this.indexedClass = indexedClass;
		this.entity = entity;
		this.elasticStore = elasticStore;
	}


	@Override
	public void run() {
		elasticStore.firePreDeleteEvent(indexedClass.getClazz(), entity);
		String docid = indexedClass.getEntityDocumentID(entity);
		DeleteResponse response = elasticStore.deleteDocument(indexedClass.getTypeName(), docid);
		if(!response.isNotFound()){
			elasticStore.firePostDeleteEvent(indexedClass.getClazz(), entity, response);
		}else{
			elasticStore.fireEntityDeleteErrorEvent(indexedClass.getClazz(), entity, response);
		}
	}
	
}
